<?php
/**
 * Created by PhpStorm.
 * User: 47557
 * Date: 2017/4/13
 * Time: 14:08
 */

namespace app\db\controller;


use app\db\model\AdminInf;
use app\db\model\PlanInf;
use app\db\model\RemenberInf;
use app\db\model\UserInf;
use app\db\model\WordgroupInf;
use app\db\model\WordInf;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use Prophecy\Exception\Exception;
use think\Controller;
use think\Db;

class Dbcommoncontrol extends Controller
  {
    protected   $pk = '';
    protected   $DbName='';
    public function GetAll()
    {
        return json( Db::table($this->DbName)->select());
    }
    public function Add()
    {
        try
        {
            $rd=$this->array_to_object(json_decode($_POST["rd"],true));//获取转数组转对象;
            $record=$this->Getnew();//获取当前控制器对应的模型的对象
            $record->data($rd);//给对象填充数据
            $record->save();//保存
            return "true";
        }
        catch (Exception $ex)
        {
            return "false";
        }
    }
    public function Alert()
    {
        try
        {
            $rd=$this->array_to_object(json_decode($_POST["rd"],true));//获取转数组转对象
            $sql=$this->Getnew()->update($rd);
            return "true";
        }
        catch (Exception $ex)
        {
            return "false";
        }
    }
    public function Delet()
    {
        try
        {
            $id=$_POST["rdid"];
            $this->Getnew()->where($this->Getnew()->key,$id)->delete();
            return "true";
        }
        catch (Exception $ex)
        {
            return "false";
        }
    }
    public function query()
    {
        try
        {
            $keystr=$_POST["keys"];
            $keyarry=explode(',',$keystr);
            $arr=[];
            foreach ($keyarry as  $key)
            {
                $arr["$key"]=$_POST[$key];
            }
            $result;
            foreach ($arr as $file =>$value)
            {
                $result= DB::table($this->DbName)->whereor($file,$value);
            }
            $data=json_encode($result->select());
            return $data;
        }
        catch (Exception $ex)
        {
            return "false";
        }

    }
    public function IdFindRecord()
    {
        $id=$_POST["Id"];
        if(isset($id)) {
           return json_encode(Db::table($this->DbName)->where($this->Getnew()->key, $id)->find());
        }
    }
    /**
       * 数组 转 对象
       *
       * @param array $arr 数组
       * @return object
       */
    public function array_to_object($arr) {
          if (gettype($arr) != 'array') {
              return;
          }
          foreach ($arr as $k => $v) {
              if (gettype($v) == 'array' || getType($v) == 'object') {
                  $arr[$k] = (object)$this->array_to_object($v);
              }
          }
          return (object)$arr;
      }
    /**
     * 获取当前控制器对应的模型的对象
     * @return AdminInf|PlanInf|RemenberInf|UserInf|WordgroupInf|WordInf
     */
    public  function   Getnew()
      {
        switch ($this->DbName)
        {
            case "AdminInf":
                return new AdminInf();
            case "WordInf":
                return new WordInf();
            case "WordGroupInf":
                return new WordgroupInf();
            case "UserInf":
                return new UserInf();
            case "RemenberInf":
                return new RemenberInf();
            case "PlanInf":
                return new PlanInf();
        }
      }
}